package ypt.fqh.maptest1;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;

import com.tencent.lbssearch.TencentSearch;
import com.tencent.lbssearch.httpresponse.HttpResponseListener;
import com.tencent.lbssearch.object.param.WalkingParam;
import com.tencent.lbssearch.object.result.RoutePlanningObject;
import com.tencent.lbssearch.object.result.WalkingResultObject;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.MapView;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TencentMapInitializer;
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.LatLngBounds;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;
import com.tencent.tencentmap.mapsdk.maps.model.PolylineOptions;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * 规划起点和终点路线
 *
 *  起点 上海市三舒路 东方康达家苑
 *  * 31.122986, 121.505669
 *
 *  终点 上海地铁8号线芦恒路
 *
 *  31.169651 121.366241
 */
public class MainActivity4 extends AppCompatActivity {


    private MapView mapView;

    private TencentMap tencentMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TencentMapInitializer.setAgreePrivacy(true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        requestDynamicPermisson();

        mapView = findViewById(R.id.mapview);
        tencentMap = mapView.getMap();

        //对地图操作类进行操作
        CameraUpdate cameraSigma =
                CameraUpdateFactory.newCameraPosition(new CameraPosition(
                        new LatLng(31.122986,121.505669),
                        15,
                        0f,
                        0f));
        //移动地图
        tencentMap.moveCamera(cameraSigma);

// 添加起点标记
        LatLng startPoint = new LatLng(31.122986, 121.505669);
//        Marker startMarker = tencentMap.addMarker(new MarkerOptions()
//                .position(startPoint)
//                .title("起点"));

// 添加终点标记
        LatLng endPoint = new LatLng(31.169651, 121.366241);
//        Marker endMarker = tencentMap.addMarker(new MarkerOptions()
//                .position(endPoint)
//                .title("终点"));


// 创建步行路线规划请求
        WalkingParam walkingParam = new WalkingParam();
        walkingParam.from(startPoint);
        walkingParam.to(endPoint);

        TencentSearch tencentSearch = new TencentSearch(getApplicationContext());
        Log.i("TAG", "checkParams:" + walkingParam.checkParams());
        tencentSearch.getRoutePlan(walkingParam, new HttpResponseListener<WalkingResultObject>() {
            @Override
            public void onSuccess(int statusCode, WalkingResultObject object) {
                System.out.println("statusCode:="+statusCode);
                if (object == null) {
                    Log.i("TAG", "baseObject为空");
                    return;
                }
                showWalkingRoute(object);
                Log.i("TAG", "message:" + object.message);
            }

            @Override
            public void onFailure(int statusCode, String responseString, Throwable throwable) {
                Log.i("TAG:" ,statusCode + "  " + responseString);
            }
        });

    }

    private void showWalkingRoute(WalkingResultObject object) {
        tencentMap.clearAllOverlays();
        if (object.result != null && object.result.routes != null && object.result.routes.size() > 0) {
            for (int i=0; i<object.result.routes.size(); i++) {
                WalkingResultObject.Route result = object.result.routes.get(i);
                tencentMap.addPolyline(new PolylineOptions().addAll(result.polyline).color(i+1).width(20));
                Log.i("TAG", "distance:" + result.distance + " duration:" + result.duration
                        + " mode:" + result.mode + " direction:" + result.direction);
                for (RoutePlanningObject.Step step : result.steps) {
                    Log.i("TAG", "step:" + step.road_name + " " + step.distance + " "
                            + step.instruction +  " " + step.act_desc + " " + step.dir_desc);
                }
                tencentMap.moveCamera(CameraUpdateFactory.newLatLngBounds(LatLngBounds.builder()
                        .include(result.polyline).build(), 100));
            }

        } else {
            Log.i("TAG", "路线结果为空");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    // 在生命周期方法中调用 MapView 对应的方法
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    private void requestDynamicPermisson() {
        //定位需要申请的权限
        String[] perms = {
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_PHONE_STATE
        };

        if (EasyPermissions.hasPermissions(this, perms)) {
            Log.i("location", "已获取权限");
        } else {
            EasyPermissions.requestPermissions(this, "必要的权限", 0, perms);
        }
    }
}